package accs.model.mail.cracker;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import accs.model.mail.MailCheckResult;
import accs.model.mail.MailChecker;

public class MailCracker {
	
	public static String checkPassword(ArrayList<String> passwordList,String user){
		MailChecker mc = new MailChecker();
		int linenumber = 0;
		for(String password : passwordList){
			linenumber++;
			if(mc.checkAdress(user,password) == MailCheckResult.VALID){
				System.out.println(password);
				return password;
			}
			if((linenumber % 100) == 0){
				System.out.print(".");
			}
		}
		return null;
	}
	
	public static ArrayList<String> readPassword(File file) throws IOException{
		BufferedReader br = new BufferedReader(new FileReader(file));	
		ArrayList<String> list = new ArrayList<String>(); 
		String line = null;
		while((line=br.readLine())!=null){
			if(!line.startsWith("#") && !line.isEmpty()){
				list.add(line);
			}
		}
		return list;
	}
	
	public static void writePasswords(File file, ArrayList<String> passwords) throws IOException {
		BufferedWriter w=new BufferedWriter(new FileWriter(file));
		
		for(String pass:passwords) {
			w.write(pass);
			w.newLine();
		}
		
		w.close();
	}
}
